package com.yidu.controller;

import com.yidu.entity.FlTb;
import com.yidu.service.FlTbService;
import com.yidu.utils.BaseMessage;
import com.yidu.utils.Tools;
import org.junit.Test;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (FlTb)表控制层
 *
 * @author JiuYue
 * @since 2021-07-29 16:28:35
 */
@RestController
@RequestMapping("flTb")
public class FlTbController {
    /**
     * 服务对象
     */
//    @Transactional  绑定事务 用的非常多
//            @Repository 绑定dao的注解
    @Resource
    private FlTbService flTbService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public BaseMessage selectOne(Integer id) {
        FlTb flTb=this.flTbService.queryById(id);
        return BaseMessage.YES("查询成功",flTb);
    }

    /**
     * 显示列表
     * @param flTb
     * @return
     */
    @GetMapping("/showList")
    public Map<String,Object> showList(FlTb flTb){
        List<FlTb>list=flTbService.queryAllByLimit(flTb);
        Map<String,Object>map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",flTbService.findCount(flTb));
        map.put("data",list);
        System.out.println(map);
        return map;
    }

    /**
     * 增加和修改
     * @param flTb
     * @return
     */
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(
            @RequestParam(value = "fqimg",required = false)
                                           MultipartFile file, FlTb flTb){
        System.out.println(flTb.getFname()+"=====================得到页面获取的鲜花名称========================");
        System.out.println(flTb.getFimage()+"=======得到的上传图片的名称========");
        try {
            String name=file.getOriginalFilename();
            System.out.println("===========================================================:"+name);
            if (!Tools.isEmpty(name)) {
                String hz = name.substring(name.lastIndexOf("."));
                String newName = UUID.randomUUID().toString() + hz;
                //将上传的文件保存到硬盘中
                file.transferTo(new File("D:\\D298\\fileUploads\\" + newName));
                flTb.setFimage("/upload/" + newName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        FlTb po=null;
        if (flTb.getFid()==null){
            po=flTbService.insert(flTb);
        }else{
            po=flTbService.update(flTb);
        }
        if (po==null){
            return BaseMessage.NO("保存失败");
        }else {
            return BaseMessage.YES("保存成功");
        }
    }


    @PostMapping("delete")
    public BaseMessage delete(Integer id) {
        Boolean flag=flTbService.deleteById(id);
        return flag?BaseMessage.YES("删除成功"):BaseMessage.NO("删除失败");
    }
    @PostMapping("/deleteBatch")
    public BaseMessage deleteBatch(String ids){
        String[] id = ids.split(",");
        int rows = flTbService.deleteByIds(id);
        return rows>0?BaseMessage.YES("删除成功"):BaseMessage.NO("删除失败");
    }

}
